<template>
  <div class="common-details">
    <customDetailsTopbar title="碳汇项目管理" flag @back="back" />
    <div id="customScroolBox" class="px-1 d-scroll">
      <div class="d-flex mt-2 w-100">
        <custom-radio-tag1 class="mb-1 w-100" :list="tagList" :value="checkedTagIndex" :check-value="checkedTag" is-scroll @change="tagChange" />
      </div>
      <customSearch ref="customSearch" placeholder="请输入项目名称" :title="`${checkedTag}列表`" :total="total" @superSearch="openSuperSearch" @search="handleSearch" />
      <div v-if="tableList.length>0" class="common_list">
        <div v-for="(item, index) in tableList" :key="index" class="list_item" @click="goDetail(item)">
          <div class="content_area">
            <div class="number_order">{{ index + 1 }}</div>
            <div class="right_content">
              <div class="content_title" style="max-width: 100%">
                {{ item.ccerProjectName || item.ccusProjectName }}
              </div>
              <ul class="des-list">
                <li v-for="(des, ind) in desList" :key="des.value" class="des">
                  <i :class="'iconfont c-text-color' + (ind + 1)" v-html="des.icon" />
                  <div class="label">{{ des.label }}：</div>
                  <div class="value">{{ item[des.value] }}</div>
                </li>
              </ul>
            </div>
          </div>
        </div>
      </div>
      <customLoadmore ref="customLoadmore" :page-num="form.pageNum" :pages="pages" @load="handlePages" />
    </div>
    <van-popup v-model="superSearch" position="right" :style="{ width: '90%', height: '100%' }" lock-scroll close-on-popstate closeable class="custom-van-pop">
      <div class="pop-vertical-flex">
        <div class="pop-content">
          <template v-if="checkedTag === 'CCER备案项目'">
            <customPopCheckbox :list="selectOptionAll['碳排放-项目类型']" :check-value.sync="form.projectType" title="项目类型" />
          </template>
          <customPopCheckbox :list="selectOptionAll['碳排放-碳汇项目']" :check-value.sync="form.projectProgress" title="项目进度" />
          <custom-timer :start.sync="form.startTime" :end.sync="form.endTime" :title="checkedTag === 'CCER备案项目' ? '备案申请时间' : checkedTag === 'CCUS项目' ? '开始日期' : '时间'" />
        </div>
        <div class="pop-bottom">
          <van-button type="default" @click="resetSuperSearch">清空</van-button>
          <van-button type="info" @click="handleSuperSearch">确认</van-button>
        </div>
      </div>
    </van-popup>
  </div>
</template>

<script>
import commonList from '@/assets/mixin/commonList'
import { ccerRecordProjectList, ccusProjectInfoList } from '@/api/carbon'
export default {
  mixins: [commonList],
  data() {
    return {
      total: 10,
      pages: 1,
      form: {
        pageNum: 1,
        pageSize: 10,
        startTime: '',
        endTime: '',
        ccusProjectName: '',
        ccerProjectName: '',
        projectType: '',
        projectProgress: ''
      },
      tagList: [
        { label: 'CCER备案项目', value: 'CCER备案项目', listFun: ccerRecordProjectList },
        { label: 'CCUS项目', value: 'CCUS项目', listFun: ccusProjectInfoList }
      ],
      listFun: ccerRecordProjectList,
      checkedTag: 'CCER备案项目',
      desListMap: {
        CCER备案项目: [
          { label: '企业名称', value: 'companyName', icon: '&#xe790;' },
          { label: '项目类型', value: 'projectType', icon: '&#xe634;' },
          { label: 'CCER项目编号', value: 'ccerProjectCode', icon: '&#xe629;' }
        ],
        CCUS项目: [
          { label: '企业名称', value: 'companyName', icon: '&#xe790;' },
          { label: '行业类别', value: 'industry', icon: '&#xe634;' },
          { label: '项目进度', value: 'projectProgress', icon: '&#xe64d;' }
        ]
      },
      checkedTagIndex: 0,
      desList: [],
      tableList: []
    }
  },
  mounted() {
    const { tag } = this.$route.query
    if (tag) {
      this.checkedTag = tag
      const checkValueIndex = this.tagList.findIndex(val => val.label === tag)
      this.checkedTagIndex = checkValueIndex
      this.listFun = this.tagList[checkValueIndex].listFun
    }
    this.desList = this.desListMap[this.checkedTag]
    this.getList()
  },
  methods: {
    back() {
      this.$router.push({
        path: '/staging/monitor',
        query: { current: this.$route.query.current }
      })
    },
    goDetail(item) {
      this.$router.push({
        path: '/staging/carbonSinkDetail',
        query: { current: this.$route.query.current, tag: this.checkedTag, data: this.checkedTag === 'CCUS项目' ? JSON.stringify(item) : item.id }
      })
    },
    tagChange(val) {
      this.checkedTag = val.item.value
      this.desList = this.desListMap[this.checkedTag]
      this.listFun = val.item.listFun
      this.tableList = []
      this.$refs.customSearch.restore()
      this.form = {
        pageNum: 1,
        pageSize: 10,
        ccusProjectName: '',
        ccerProjectName: ''
      }
      this.getList()
    },
    async getList() {
      this.$refs.customLoadmore.loadStatus = true
      const { status, data } = await this.listFun(this.form)
      if (status === 0 && data) {
        this.total = data.total
        this.tableList = this.tableList.concat(data.list)
        this.pages = data.pages
      }
      this.$refs.customLoadmore.loadStatus = false
    },
    handleSearch(value) {
      this.form.ccusProjectName = this.form.ccerProjectName = value
      this.form.pageNum = 1
      this.tableList = []
      this.getList()
    },
    handlePages(value) {
      this.form.pageNum = value
      this.getList()
    },
    getStart(val) {
      this.form.startTime = val
    },
    getEnd(val) {
      this.form.endTime = val
    },
    resetSuperSearch() {
      this.form.startTime = ''
      this.form.endTime = ''
      this.form.projectType = ''
      this.form.projectProgress = ''
    },
    handleSuperSearch() {
      this.form.pageNum = 1
      this.tableList = []
      this.superSearch = false
      this.getList()
    }
  }
}
</script>

<style lang="scss" scoped>
.checkbox-title {
  font-size: 28px;
  font-weight: bold;
  margin-bottom: 30px;
}

.right_content {
  .des-list {
    display: flex;
    flex-wrap: wrap;
    .des {
      width: 100%;
    }
  }
}
</style>
